/*
 * 从excel读数据，存入mysql
 * 主入口
 * 文件路径、表名可指定 
 */

package view;

import context.ExcelConnection;
import dao.Excel03DAO;
import dao.Excel07DAO;
import exception.ViewException;

public class ExcelAccess {

	public ExcelAccess(){
		
	}
	
	public String getFileName(String excelAdd) {
		String file = excelAdd.substring(excelAdd.lastIndexOf("\\")+1, excelAdd.lastIndexOf("."));
		System.out.println(file);
		return file;
	}
	
	public ExcelAccess(String excelAdd) throws ViewException{
		
		ExcelConnection connection = ExcelConnection.newInstance();
		
		connection.setAddr(excelAdd);
		String tableName = getFileName(excelAdd);
		
		//判读输入文件类型，并选择合适的DAO
		if(getFileType(excelAdd).equals("xls")){	
			Excel03DAO.importDataNew(tableName);
			
		}
		else if(getFileType(excelAdd).equals("xlsx")){
			Excel07DAO.getExcelRecord(tableName);
			
		}
		else 
		{
			System.out.println("input file type error!");
		}	
		
	}
	
//	public static void main(String args[]){
//		
//		//用户指定读取Excel文件路径
//		String excelAdd = "C:/Users/chl/Desktop/easi/test11.xls";
//		
//		//用户输入数据库新建表名
//		String tableName = "NEWPAYRECORD";
//		
//		ExcelConnection connection = ExcelConnection.newInstance();
//		
//		connection.setAddr(excelAdd);
//		
//		ExcelDAO.getExcelRecord(tableName);
//		
//		System.out.println("access already!");
//	}
	
	//String tableName = "NEWPAYRECORD";
	
	
	private static String getFileType(String s) {

		String[] ss = new String[40];

		for (int i = 0; i < ss.length; i++) {

			ss = s.split("\\.");


		}
		return ss[1];
	}
	
	
}
